using Microsoft.AspNetCore.Mvc;
using Lzfy_Refund_Service.Models;
using Lzfy_Refund_Service.Services;

namespace Lzfy_Refund_Service.Controllers
{
    /// <summary>
    /// 退费任务完成情况控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class RefundTaskCompletionStatusController : ControllerBase
    {
        private readonly IRefundTaskCompletionStatusService _service;
        private readonly ILogger<RefundTaskCompletionStatusController> _logger;

        public RefundTaskCompletionStatusController(
            IRefundTaskCompletionStatusService service,
            ILogger<RefundTaskCompletionStatusController> logger)
        {
            _service = service;
            _logger = logger;
        }

        /// <summary>
        /// 获取所有退费任务完成情况
        /// </summary>
        /// <returns>退费任务完成情况列表</returns>
        [HttpPost("get-all")]
        public async Task<ActionResult<RefundTaskCompletionStatusQueryResponse>> GetAll()
        {
            try
            {
                _logger.LogInformation("开始获取所有退费任务完成情况");
                var items = await _service.GetAllAsync();
                var response = new RefundTaskCompletionStatusQueryResponse
                {
                    Items = items.ToList(),
                    TotalCount = items.Count()
                };
                
                _logger.LogInformation("获取所有退费任务完成情况成功，共 {Count} 条记录", response.TotalCount);
                return Ok(response);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取所有退费任务完成情况失败");
                return StatusCode(500, new { message = "获取退费任务完成情况失败", error = ex.Message });
            }
        }

        /// <summary>
        /// 根据用户账号获取退费任务完成情况
        /// </summary>
        /// <param name="request">包含用户账号的请求</param>
        /// <returns>退费任务完成情况列表</returns>
        [HttpPost("get-by-username")]
        public async Task<ActionResult<RefundTaskCompletionStatusQueryResponse>> GetByUserName([FromBody] GetByUserNameRequest request)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(request?.UserName))
                {
                    return BadRequest(new { message = "用户账号不能为空" });
                }

                _logger.LogInformation("开始获取用户 {UserName} 的退费任务完成情况", request.UserName);
                var items = await _service.GetByUserNameAsync(request.UserName);
                var response = new RefundTaskCompletionStatusQueryResponse
                {
                    Items = items.ToList(),
                    TotalCount = items.Count()
                };
                
                _logger.LogInformation("获取用户 {UserName} 的退费任务完成情况成功，共 {Count} 条记录", request.UserName, response.TotalCount);
                return Ok(response);
            }
            catch (ArgumentException ex)
            {
                _logger.LogWarning(ex, "获取退费任务完成情况参数错误: {Message}", ex.Message);
                return BadRequest(new { message = ex.Message });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取用户 {UserName} 的退费任务完成情况失败", request?.UserName);
                return StatusCode(500, new { message = "获取退费任务完成情况失败", error = ex.Message });
            }
        }

        /// <summary>
        /// 根据用户姓名获取退费任务完成情况
        /// </summary>
        /// <param name="request">包含用户姓名的请求</param>
        /// <returns>退费任务完成情况列表</returns>
        [HttpPost("get-by-peoplename")]
        public async Task<ActionResult<RefundTaskCompletionStatusQueryResponse>> GetByPeopleName([FromBody] GetByPeopleNameRequest request)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(request?.PeopleName))
                {
                    return BadRequest(new { message = "用户姓名不能为空" });
                }

                _logger.LogInformation("开始获取姓名包含 {PeopleName} 的退费任务完成情况", request.PeopleName);
                var items = await _service.GetByPeopleNameAsync(request.PeopleName);
                var response = new RefundTaskCompletionStatusQueryResponse
                {
                    Items = items.ToList(),
                    TotalCount = items.Count()
                };
                
                _logger.LogInformation("获取姓名包含 {PeopleName} 的退费任务完成情况成功，共 {Count} 条记录", request.PeopleName, response.TotalCount);
                return Ok(response);
            }
            catch (ArgumentException ex)
            {
                _logger.LogWarning(ex, "获取退费任务完成情况参数错误: {Message}", ex.Message);
                return BadRequest(new { message = ex.Message });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取姓名包含 {PeopleName} 的退费任务完成情况失败", request?.PeopleName);
                return StatusCode(500, new { message = "获取退费任务完成情况失败", error = ex.Message });
            }
        }

        /// <summary>
        /// 根据查询条件获取退费任务完成情况
        /// </summary>
        /// <param name="request">查询请求</param>
        /// <returns>退费任务完成情况列表</returns>
        [HttpPost("get-by-conditions")]
        public async Task<ActionResult<RefundTaskCompletionStatusQueryResponse>> GetByConditions([FromBody] RefundTaskCompletionStatusQueryRequest request)
        {
            try
            {
                if (request == null)
                {
                    return BadRequest(new { message = "查询请求不能为空" });
                }

                _logger.LogInformation("开始根据条件获取退费任务完成情况，用户账号: {UserName}, 用户姓名: {PeopleName}", 
                    request.UserName, request.PeopleName);
                var items = await _service.GetByConditionsAsync(request);
                var response = new RefundTaskCompletionStatusQueryResponse
                {
                    Items = items.ToList(),
                    TotalCount = items.Count()
                };
                
                _logger.LogInformation("根据条件获取退费任务完成情况成功，共 {Count} 条记录", response.TotalCount);
                return Ok(response);
            }
            catch (ArgumentException ex)
            {
                _logger.LogWarning(ex, "获取退费任务完成情况参数错误: {Message}", ex.Message);
                return BadRequest(new { message = ex.Message });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "根据条件获取退费任务完成情况失败，用户账号: {UserName}, 用户姓名: {PeopleName}", 
                    request?.UserName, request?.PeopleName);
                return StatusCode(500, new { message = "获取退费任务完成情况失败", error = ex.Message });
            }
        }
    }

    /// <summary>
    /// 根据用户账号查询请求
    /// </summary>
    public class GetByUserNameRequest
    {
        /// <summary>
        /// 用户账号
        /// </summary>
        public string UserName { get; set; } = string.Empty;
    }

    /// <summary>
    /// 根据用户姓名查询请求
    /// </summary>
    public class GetByPeopleNameRequest
    {
        /// <summary>
        /// 用户姓名
        /// </summary>
        public string PeopleName { get; set; } = string.Empty;
    }
}